package org.jeecg.qis.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.qis.entity.QisProcheadValue;
import org.jeecg.qis.entity.QisProcrecord;
import org.jeecg.qis.mapper.QisProcdetailValueMapper;
import org.jeecg.qis.mapper.QisProcheadValueMapper;
import org.jeecg.qis.service.IQisProcheadValueService;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 过程检验单主表
 * @Author: shaolong
 * @Date: 2022-10-11
 * @Version: V1.0
 */
@Service
public class QisProcheadValueServiceImpl extends ServiceImpl<QisProcheadValueMapper, QisProcheadValue> implements IQisProcheadValueService {

    @Autowired
    private QisProcheadValueMapper qisProcheadValueMapper;
    @Autowired
    private QisProcdetailValueMapper qisProcdetailValueMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        qisProcdetailValueMapper.deleteByMainId(id);
        //把这里的删除给改了 改成按照id删
        Map<String, Object> map = new HashMap<>();
        map.put("REC_ID", id);
        qisProcheadValueMapper.deleteByMap(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            qisProcdetailValueMapper.deleteByMainId(id.toString());
            qisProcheadValueMapper.deleteById(id);
        }
    }

    @Override
    public Result<String> edit(QisProcheadValue qisProcheadValue) {
        LambdaUpdateWrapper<QisProcheadValue> eq = new LambdaUpdateWrapper<QisProcheadValue>();
        //自定义更新条件   .eq(MesLineCfg::getLine, mesLineCfg.getLine());
        boolean update = update(qisProcheadValue, eq);
        return update ? Result.ok("修改成功!") : Result.error("修改失败!");
    }

    @Override
    public List<Map<String, String>> queryOpList(QisProcrecord qisProcrecord) {
        return this.baseMapper.queryOpList(qisProcrecord);
    }

    @Override
    public void saveHeadsById(List<String> uniqueHeadIds) {
        uniqueHeadIds.forEach(item->{
            //查询这个id里边的子类是否有未检验的或者有未完成的
            String result =this.baseMapper.getHeadResult(item);
            //修改状态
            LambdaUpdateWrapper<QisProcheadValue> updateWrapper = new LambdaUpdateWrapper<QisProcheadValue>()
                    .set(QisProcheadValue::getResult, result)
                    .eq(QisProcheadValue::getRecId, item);
            this.update(updateWrapper);
        });
    }

}
